// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of CImplCopy.
//   $Id$

#ifndef SELECT_H_INCLUDED
#define SELECT_H_INCLUDED 1

#define OLD_SELECT_METHOD 1

#include "SingleNodeBinding.h"
#include "resolvedItem.h"
#include "resolvedItemList.h"
#include "multipleTextsource.h"
#include <list>

class CXFormsElementSelection;

typedef CTypedPtrList<CPtrList, ISelectionChild*> CItemList;


class CImplCopy : public CImplSingleNodeBinding
{
public:
	CImplCopy() { };
	virtual ~CImplCopy();
	//refreshing a copy does nothing, it is passive,
	//	and is refreshed by itemset.
	virtual HRESULT refresh(){return S_OK;};

};

class CImplValue : public CImplMultipleTextSource
{
public:
	CImplValue();
	virtual ~CImplValue() ;

	virtual HRESULT refresh();
	virtual HRESULT init(CImpl * pParent);

};


class CXFormsParseSelection;
#include "ImplFormControlInput.h"

class CImplSelection 
	: public CImplFormControlInput
	, public IResolvedItemContainer

{
	friend class CXFormsParseSelection;
public:
	CImplSelection(CXFormsElementSelection* pElement);
	virtual ~CImplSelection();

	void AddItem(ISelectionChild* pItem);
	void RemoveItem(ISelectionChild* pItem);
	virtual HRESULT refresh();
	virtual bool checkDrawn();
	virtual HRESULT onactivate();

	virtual CResolvedItemList * GetResolvedItemList();

protected:
	virtual HRESULT formControlInitialize();
	CItemList m_Items;
	CMapStringToPtr m_Items_flat;
	
	CMapStringToPtr* m_pCurrentlySelectedItems;
	bool IsOpen();	
private:
	CComBSTR m_sSelection;
	CResolvedItemList m_ResolvedItems;
	bool m_bIsDrawn;
	long m_lItemCount;
public:
	// hack to implement copy
	HRESULT drawItem(ISelectionChild * pItem , const BSTR sId, const BSTR sCaption, const BSTR sValue, const BSTR sDrawInside, bool bClear);
	virtual HRESULT putValue(const BSTR bsValue);
	void appendValue(const BSTR sValue);
	// <summary>clones the given node and appends it to the instance node.</summary>
	void appendCopy(IXMLDOMNode * pNodeToCopy);
private:
	// a fragment to build during selection changes, which is then inserted into the instance node.
	IXMLDOMDocumentFragment *m_pFragment;
	//whether text has sbeen added to the fragment, so that space sepatation occurs.
	bool m_bTextFragmentAdded;
public:
	void FindAndSelect(CList<VARIANT, VARIANT> &lstNodes);
protected:
	bool m_bPutValue;

	bool m_bRefreshing;
};


class CXFormsElementSelectOne;

class CImplSelectOne : public CImplSelection
{
public:
#ifdef _DEBUG
	virtual HRESULT AddChild(CImpl * pChild);
#endif
	CImplSelectOne(CXFormsElementSelectOne* pElement);
	virtual ~CImplSelectOne();
	virtual HRESULT refresh();

private:
	void DoGutsOfRefresh(IXMLDOMNodeList *pNL, BSTR sText);
};

#endif
